import { Component, OnInit, EventEmitter, Input, Output } from '@angular/core';
import { SearchBarSet } from '../..';
import { SearchBarSetadmin } from '../..';
import { ExpressionPair, Compare, AndOr } from '../../core/model/CommonDto';
import { CityListComponent } from '../../system/school/cityList';
import { HttpService } from '../../core/service/http.service';
import { AlertPromise } from '../../../../node_modules/@types/selenium-webdriver';
/**
 * 搜索组件，用于管理员和用户的模块信息搜索
 * ### 模块功能要点
 * * 主要业务逻辑
 *  1. 根据用户权限不同，显示不同的搜索内容，`校管`:系、名称搜索,`超管`：全部功能
 *  2. 可以触发父组件的搜索功能:`dataListFilter($event)`
 *  3. 绑定父组件搜索结果属性：`listSearchValue: Array<ExpressionPair> = new Array<ExpressionPair>();`
 *  4. 用户需要指定查询框中的查询字段名称。
 *
 * ### 举个栗子
 * ```
 *  <app-search-bar-widget  (searchEvent)='searchBar($event)' [searchTextFieldName]='"personName"'></app-search-bar-widget>
 * ```
 * @export 输出成Angular组件
 * @class SearchBarWidgetComponent
 * @implements {OnInit}
 */
@Component({
  selector: 'app-search-bar-widget',
  templateUrl: './search-bar-widget.component.html',
  styleUrls: ['./search-bar-widget.component.scss']
})
export class SearchBarWidgetComponent implements OnInit {

  @Input()
  searchTextFieldName: string;

  @Output()
  searchEvent = new EventEmitter();
  // 大管理员登入
  searchBarSet = new SearchBarSet();
  // 院校管理登入
  searchBarSetadmin = new SearchBarSetadmin();

  // 是否是超管
  superAdmin = false;
  // 城市列表
  cityData = {};
  // 省份列表
  provinceData = [];
  // 学校列表
  listSchool = [];
  // 学院信息
  listAcademe = [];
  // 系信息
  listDepartment = [];
  //
  // 身份
  usertype;
  // 超管
  where;

  // 院管理
  whereadmin;

  // 院id
  academeId;

  listDepartmentadmin;

  constructor(
    private http: HttpService
  ) {
    // 根据服务获取登陆账号信息。
    this.superAdmin = true;
    // 城市信息绑定
    if (this.superAdmin) {
      const city = new CityListComponent();
      this.cityData = city.cityDatas;
      this.provinceData = city.provinceDatas;
    }
  }

    async ngOnInit() {
    this.usertype = sessionStorage.getItem('userRoleType'); // 身份
    this.academeId = sessionStorage.getItem('academeId'); // 身份
    // 初始化文本框搜索字段内容
    if (this.usertype === "0") {
      this.searchBarSet.searchText.PropertyName = this.searchTextFieldName;
      this.searchBarSet.provinceName.propertyValue = undefined;
      this.searchBarSet.cityName.propertyValue = undefined;
      this.searchBarSet.school.propertyValue = undefined;
      this.searchBarSet.academe.propertyValue = undefined;
      this.searchBarSet.department.propertyValue = undefined;
    } else {
       const data = { "academeId": this.academeId, "collegeDepartmentName": "" };
        this.listDepartmentadmin = await this.http.post<any>(`School/GetDepartmentByAcademe`, data);

      this.searchBarSetadmin.searchTextadmin.PropertyName = this.searchTextFieldName;
      this.searchBarSetadmin.departmentadmin.propertyValue = undefined;
      this.searchBarSetadmin.searchTextadmin.propertyValue = undefined;
    }
  }

  async getSchool() {
    const searchName = this.searchBarSet.provinceName.propertyValue + "-" + this.searchBarSet.cityName.propertyValue;
    this.listSchool = await this.http.get<any>(`School/GetSchoolByAddress?address=${searchName}`);
    // this.searchBarSet.school.propertyValue = '';
    // this.searchBarSet.academe.propertyValue = '';
    // this.searchBarSet.department.propertyValue = '';

    this.searchBarSet.school.propertyValue = undefined;
    this.searchBarSet.academe.propertyValue = undefined;
    this.searchBarSet.department.propertyValue = undefined;
  }

  async getAcademe(id: number) {
    this.listAcademe = await this.http.get<any>('School/GetAllAcademeBySchool?input=' + id);
    // this.searchBarSet.academe.propertyValue = '';
    // this.searchBarSet.department.propertyValue = '';

    this.searchBarSet.academe.propertyValue = undefined;
    this.searchBarSet.department.propertyValue = undefined;
  }

  async getDepartment(id: number) {

    const data = { "academeId": id, "collegeDepartmentName": "" };

    this.listDepartment = await this.http.post<any>(`School/GetDepartmentByAcademe`, data);
    // console.log(id);
    // console.log(this.listDepartment);

  }

  /**
   * 重置组件选择状态
   *
   * @param {boolean} isProvince
   * @memberof SearchBarWidgetComponent
   */
  resetAll(isProvince: boolean) {
    if (!isProvince) {
      // this.searchBarSet.provinceName.propertyValue = '';
      this.searchBarSet.provinceName.propertyValue = undefined;
    }
    // this.searchBarSet.cityName.propertyValue = '';
    // this.searchBarSet.school.propertyValue = '';
    // this.searchBarSet.academe.propertyValue = '';
    // this.searchBarSet.department.propertyValue = '';

    this.searchBarSet.cityName.propertyValue = undefined;
    this.searchBarSet.school.propertyValue = undefined;
    this.searchBarSet.academe.propertyValue = undefined;
    this.searchBarSet.department.propertyValue = undefined;
    // 院管理
    this.searchBarSetadmin.departmentadmin.propertyValue = undefined;
    this.searchBarSetadmin.searchTextadmin.propertyValue = undefined;
  }

  /**
   * 查询数据
   *
   * @memberof SearchBarWidgetComponent
   * @param [isReset] 是否是重置（取消）查询操作
   */
  async search(isReset: boolean) {
    if (isReset) {
      this.resetAll(false);
    }
    if (this.usertype === '0') {
      this.where = new Array<any>(
        this.searchBarSet.academe,
        this.searchBarSet.cityName,
        this.searchBarSet.department,
        this.searchBarSet.provinceName,
        this.searchBarSet.school,
        this.searchBarSet.searchText
      );
      this.searchEvent.emit(this.where);
    } else {
      this.whereadmin = new Array<any>(
        this.searchBarSetadmin.departmentadmin,
        this.searchBarSetadmin.searchTextadmin
      );
      this.searchEvent.emit(this.whereadmin);
    }
  }
  // 输入回车判断是否搜索
  searchadmin(e) {
    if (e.keyCode === 13) {
      this.search(false);
    }
  }
}
